<?php
namespace app\web\admin;

use app\common\builder\ZBuilder;
use app\lib\model\Price;
use app\web\model\Platform;

/**
 * 
 */
class Capital extends Base
{

    public function index()
    {
        $map = $this->getMap();
        $list = Platform::where($map)->order('id','desc')->paginate();
        $income = Platform::where($map)->where('change_balance','>',0)->sum('change_balance');
        $expenditure = Platform::where($map)->where('change_balance','<',0)->sum('change_balance');
        $expenditure = abs($expenditure);
        $balance = Price::pricecalc($income,'-',$expenditure);
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->setTableName('platform')
            ->addColumns([
                ['role','用户类型','status','',['用户','技师','商户']],
                ['platform_type','变动类型','status','',Platform::$platform_type],
                ['mobile','手机'],
                ['user_name','昵称'],
                ['before_balance','变动前金额'],
                ['change_balance','变动金额'],
                ['after_balance','变动后金额'],
                ['notes','备注'],
                ['create_time','变动时间']
            ])
            ->assign(['income'=>$income,'expenditure'=>$expenditure,'balance'=>$balance])
//            ->setExtraHtml($html,'toolbar_bottom')
            ->js("printer")
            ->addTopButtons([
                'myexport' => ['title'=>'导出','class'=>'btn btn-primary ajax-post mycapitalexport-btn','href'=>'javascript:void(0);'],
            ])
            ->addTimeFilter('create_time')
            ->addFilterList('role',['用户','技师','商户'])
            ->addFilterList('platform_type',Platform::$platform_type)
            ->setRowList($list)
            ->fetch('edit');
    }
    public function myexport(){
        $map = $this->getMap();
        $shop_list = Platform::where($map)
            ->order('id')
            ->select()->each(function($item,$k){
                $item['role'] = ['用户','技师','商户'][$item['role']];
                $item['platform_type'] = Platform::$platform_type[$item['platform_type']];
            });

        $cellName = [
            ['role','18','用户类型'],
            ['platform_type', '20', '变动类型'],
            ['mobile','18','手机号'],
            ['user_name','20','昵称'],
            ['before_balance','15','变动前金额'],
            ['change_balance','15','变动金额'],
            ['after_balance','15','变动后金额'],
            ['notes','30','备注'],
            ['create_time','30','变动时间'],
        ];
        plugin_action('Excel/Excel/export', ['pending_money', $cellName, $shop_list]);
    }
}